package com.ai.bitcoin.weixin.template;

import com.ai.bitcoin.constant.WeiXinConfig;
import com.ai.bitcoin.service.helper.WeiXinServiceHelper;
import com.ai.bitcoin.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SendReportService {

    private static final Map<String,String> headerMap = new HashMap<String, String>();

    static {
        headerMap.put("Content-Type", "application/json");
    }

    @Autowired
    private WeiXinServiceHelper weiXinServiceHelper;

    public void sendTemplateMsg(Map<String,Object> parameterMap) {
        if(parameterMap == null || parameterMap.isEmpty()) {
            return;
        }
        String accessToken = weiXinServiceHelper.getWeiXinAccessToken();
        StringBuilder url = new StringBuilder(
                WeiXinConfig.TEMPLATE_MSG_URL);
        url.append("?access_token=").append(accessToken);
        String resp = null;
        try {
            resp = HttpClientUtils.doPost(url.toString(), parameterMap, headerMap);
        } catch (Exception e) {
            log.error("unexpected error for sending template msg {}", e);
            throw new RuntimeException(
                    "unexpected error for sending template msg");
        }
        log.info("send template msg { "+resp+" }");
    }
}
